package com.ws.shop.service.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ws.shop.bean.Page;
import com.ws.shop.dao.SunpicDao;
import com.ws.shop.entity.Accessory;
import com.ws.shop.entity.Sunpic;
import com.ws.shop.service.SunpicService;

@Service(value="sunpicService")
public class SunpicServiceImpl implements SunpicService {

	@Resource(name="sunpicDao")
	private SunpicDao sunpicDao;
	
	@Cacheable(value="sunpicColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Object> sunpics(int state, int limit) throws SQLException {
		return sunpicDao.sunpics(state, limit);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findListByPage(int pageno, int pagesize, String name,long aid, String start,
			String end) {
		return sunpicDao.findListByPage(pageno, pagesize, name, aid, start, end);
	}
	
	@Cacheable(value="sunpicColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page sunpicsPage(final int pageno,int pagesize,final int state){
		 return sunpicDao.sunpicsPage(pageno,pagesize, state);
	}

	public void getFreeBack(final Page page,final long uid){
		sunpicDao.getFreeBack(page, uid);
	}
	
	@CacheEvict(value="sunpicColl",allEntries=true)
	public int saveSunpic(final long uid,final long id,final int state,final String word,final String[] links){
		return sunpicDao.saveSunpic(uid, id, state, word, links);
	}
	
	public List<Object> sunpics(final int state ,final long uid, final int limit){
		try {
			return sunpicDao.sunpics(state,uid, limit);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public Sunpic sunpics(final int state ,final long id) {
		return sunpicDao.sunpics(state,id);
	}
	
	public  List<Sunpic>  getSunpicByUidAndOid(final long uid,final long oid){
		return sunpicDao.getSunpicByUidAndOid(uid, oid);
	}

	@Cacheable(value="sunpicColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Accessory> findAccessoryListBySunpicId(long id) {
		return sunpicDao.findAccessoryListBySunpicId(id);
	}

	@Cacheable(value="sunpic",key="'findByAid_'+#aid")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Sunpic findSunpicByAid(long aid) {
		return sunpicDao.findSunpicByAid(aid);
	}
	
}
